﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Future.FormBase;
using System.Collections;

namespace Future.CRMClient
{
    public partial class frmMain : frmBase, IMdiForm
    {
        //注册不同类型的ToolBar,目录使用DevBarRegister注册器 
        private IToolbarRegister _MdiToolbar = null;

        /// <summary>
        /// 模块管理器实例
        /// </summary>
        private ModuleManager _ModuleManager;

        /// <summary>
        /// 模块主界面容器
        /// </summary>
        private frmModuleContainer _ModuleContainer;

        /// <summary>
        /// 保存各模块的主菜单
        /// </summary>
        private MenuStrip _moduleMenus = new MenuStrip();

        public frmMain()
        {
            InitializeComponent();

            SkinTools.LoadSkinList(btnSkin);
        }

        /// <summary>
        /// MDI主窗体的工具栏
        /// </summary>
        public IToolbarRegister MdiToolbar
        {
            get
            {
                return _MdiToolbar;
            }
            set
            {
                _MdiToolbar = value;
            }
        }

        /// <summary>
        /// MDI主窗体观察者列表
        /// </summary>
        public IObserver[] MdiObservers
        {
            get
            {
                IObserver[] os = new IObserver[2];
                os[0] = new ObserverCloseAllChild(this);
                return os;
            }
        }

        /// <summary>
        /// 注册MDI主窗体功能的按钮
        /// </summary>
        public void RegisterMdiButtons()
        {
            this._MdiToolbar.RegisterButton(MdiButtons);
        }

        /// <summary>
        /// 主窗体的按钮
        /// </summary>
        public IList MdiButtons
        {
            get
            {
                IList btns = new ArrayList();
                btns.Add(this.MdiToolbar.CreateButton("btnHelp", "帮助",
                    Resource._24_Help.ToBitmap(), new Size(57, 28), this.DoHelp));

                btns.Add(this.MdiToolbar.CreateButton("btnAbout", "关于程序",
                     Resource.cslogo24.ToBitmap(), new Size(57, 28), this.DoHelp));

                btns.Add(this.MdiToolbar.CreateButton("btnClose", "关闭程序",
                    Resource._24_Exit.ToBitmap(), new Size(57, 28), this.DoHelp));

                //btns.Add(this.MdiToolbar.CreateButton("btnLsit", "列表样式",
                //     Resource.list.ToBitmap(), new Size(57, 28), this.DoHelp));

                //btns.Add(this.MdiToolbar.CreateButton("btnTree", "树形样式",
                //     Resource.tree.ToBitmap(), new Size(57, 28), this.DoHelp));

                //添加所有窗体共用的工具栏

                return btns;
            }
        }

        public void DoHelp(IButtonInfo sender)
        {
            Common.Msg.ShowInformation("显示系统帮助");
        }

        /// <summary>
        /// 所有模块主菜单的集合
        /// </summary>
        public MenuStrip MainMenu
        {
            get { return _moduleMenus; }
        }

        public void ShowModuleContainerForm()
        {
            _ModuleContainer.Activate();
        }

        public void InitUserInterface(StatusLable splash)
        {
            this.SuspendLayout();

            splash.UpdateMessage("正在初始化用户界面...");
            this._MdiToolbar = new DevBarRegister(this.bar_tools, this);
            this.RegisterMdiButtons();

            splash.UpdateMessage("正在初始化模块容器...");
            _ModuleContainer = (frmModuleContainer)MdiTools.OpenChildForm(this as IMdiForm, typeof(frmModuleContainer), null);
            _ModuleContainer.InitButtons();

            splash.UpdateMessage("正在加载模块...");
            _ModuleManager = new ModuleManager(this, _ModuleContainer.xtraTabControl1);
            _ModuleManager.LoadModules(splash, _moduleMenus);

            splash.UpdateMessage("正在加载工具栏...");
            _ModuleManager.CreateToolButtons(bar_menu, _moduleMenus);

            splash.UpdateMessage("正在加载导航面板(Navigator Panel)...");
            _ModuleManager.CreateNavBarButtons(navBarControl1, _moduleMenus, NavigatorStyle.BarContainer);

            splash.UpdateMessage("加载完毕.");
            this.ResumeLayout();
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
